home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / admin / psf < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  2.5 KB  |  120 lines

  1. #!/bin/ksh
  2. # @(#) psf.ksh 1.0 93/11/19
  3. # 93/11/09 John H. DuBois III (john@armory.com)
  4. # 93/11/19 Get rid of c, p, r codes at end of PIDs
  5. #          Use $(ps) instead of ps|read so that leading whitespace isn't lost
  6. #          Added options (-h and -x)
  7.  
  8. name=${0##*/}
  9. Usage="Usage: $name [-hx] file ..."
  10.  
  11. typeset -i i=0 j numfiles debug=0
  12.  
  13. alias istrue="test 0 -ne"
  14. alias isfalse="test 0 -eq"
  15.  
  16. while getopts :hx opt; do
  17.     case $opt in
  18.     h)
  19.     echo \
  20. "$name: list processes using files.
  21. $Usage
  22. A ps line is printed for any process that has any of the named files open.
  23. Options:
  24. -h: Print this help.
  25. -x: Turn on debugging."
  26.     exit 0
  27.     ;;
  28.     x)
  29.     debug=1
  30.     ;;
  31.     +?)
  32.     print -u2 "$name: options should not be preceded by a '+'."
  33.     exit 1
  34.     ;;
  35.     :)
  36.         print -r -u2 -- \
  37.         "$name: Option '$OPTARG' requires a value.  Use -h for help."
  38.         exit 1
  39.         ;;
  40.     ?) 
  41.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  42.     exit 1
  43.     ;;
  44.     esac
  45. done
  46.  
  47. # remove args that were options
  48. let OPTIND=OPTIND-1
  49. shift $OPTIND
  50.  
  51. if [ $# -lt 1 ]; then
  52.     print -u2 "$Usage\nUse -h for help."
  53.     exit
  54. fi
  55.  
  56. # Generate process ID list
  57. OIFS=$IFS
  58. # Need to get error output with standard output so that we can
  59. # keep process lists associated with files
  60. fuser $* 2>&1 | while read file proclist; do
  61.     set -- $proclist
  62.     if [ -z "$proclist" ]; then
  63.     print -u2 "$file no processes"
  64.     elif [[ "$1" != [0-9]* ]]; then
  65.     # If a word starting with anything other than a digit is printed
  66.     # after a  file name, it is the start of an error message
  67.     print -u2 "$file $proclist"
  68.     else
  69.     istrue debug && print -u2 "$file $proclist"
  70.     files[i]=${file%:}
  71.  
  72.     j=0
  73.     unset pids[*]
  74.     for pid; do
  75.         pids[j]=${pid%[!0-9]}    # get rid of file use code (c, p, r)
  76.         let j+=1
  77.     done
  78.     # Make the PID list be an egrep pattern: pid1|pid2|pid3...
  79.     IFS=\|
  80.     procpat[i]="${pids[*]}"
  81.     IFS=$OIFS
  82.  
  83.     allprocs="$allprocs ${pids[*]}"
  84.     let i+=1
  85.     fi
  86. done
  87. istrue debug && print "PIDs: $allprocs"
  88. [ i -lt 1 ] && exit
  89. numfiles=i
  90.  
  91. # Set ps args.  Use -e instead of -fp because ps has a 20 proc limit
  92. set -- $allprocs
  93. [ $# -le 20 ] && set -- -fp "$allprocs" || set -- -fe
  94.  
  95. # Get ps output for each process ID
  96. IFS="
  97. "
  98. # Use this instead of read because read always strips leading whitespace
  99. set -- $(ps "$@")
  100. IFS=$OIFS
  101. for line in "$@"; do
  102.     set -- $line
  103.     pid=$2
  104.     i=0
  105.     while [ i -lt numfiles ]; do
  106.     if eval [[ $pid = "@(${procpat[i]})" ]]; then
  107.         pslines[i]="${pslines[i]}
  108. $line"
  109.     fi
  110.     let i+=1
  111.     done
  112. done
  113.  
  114. # Print output by filename
  115. i=0
  116. while [ i -lt numfiles ]; do
  117.     print "${files[i]}:${pslines[i]}"
  118.     let i+=1
  119. done
  120.